package com.easy.dn;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.easy.common.base.BaseActivity;
import com.easy.dn.app.AppConstant;
import com.easy.dn.bean.TabBean;
import com.easy.dn.ui.mainFragment.ChatFragment;
import com.easy.dn.ui.mainFragment.HomeFragment;
import com.easy.dn.ui.mainFragment.MyPublishFragment;
import com.easy.dn.ui.mainFragment.PublishFragment;
import com.easy.dn.ui.mainFragment.UserCenterFragment;
import com.easy.dn.utils.DnUtils;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.flyco.tablayout.utils.UnreadMsgUtils;
import com.flyco.tablayout.widget.MsgView;

import java.util.ArrayList;

import butterknife.Bind;
import dn.easy.com.easyto.R;

public class MainActivity extends BaseActivity {

    @Bind(R.id.viewpager)
    ViewPager mViewpager;
    @Bind(R.id.tablayout)
    CommonTabLayout mTablayout;
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private String[] mTitles = {"首页", "我的发布", "聊天", "发布", "个人中心"};
    private int[] mIconUnselectIds = {
            R.drawable.tab_home_unselect, R.drawable.tab_my_publish_unselect, R.drawable.tab_speech_unselect,
            R.drawable.tab_publish_unselect, R.drawable.tab_contact_unselect};
    private int[] mIconSelectIds = {
            R.drawable.tab_home_select, R.drawable.tab_my_publish_select, R.drawable.tab_speech_select,
            R.drawable.tab_publish_select, R.drawable.tab_contact_select};

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabBean(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        mFragments.add(new HomeFragment());
        mFragments.add(new MyPublishFragment());
        mFragments.add(new ChatFragment());
        mFragments.add(new PublishFragment());
        mFragments.add(new UserCenterFragment());
        MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        mViewpager.setAdapter(mAdapter);
        setData();
    }

    private void setData() {
        mTablayout.setTabData(mTabEntities);
        mTablayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewpager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                AppConstant.WHERETOGO = position;
                mTablayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mViewpager.setCurrentItem(AppConstant.WHERETOGO);
        //设置未读消息红点
        mTablayout.showDot(2);
        MsgView rtv_2_2 = mTablayout.getMsgView(2);
        if (rtv_2_2 != null) {
            UnreadMsgUtils.setSize(rtv_2_2, dp2px(7.5f));
        }
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

    protected int dp2px(float dp) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DnUtils.dataLsit = null;
        DnUtils.mProfessionTypeList = null;
    }
}
